<!--  -->
<template>

  <div class="todos">
    <!-- 标题开始 -->
    <div class="todos-header">
      <h1>todos</h1>
    </div>
    <!-- 标题结束 -->
    <!-- 内容开始 -->
    <div class="todos-main">
      <!-- 顶部开始 -->
      <div class="todos-main-top">
        <div :class="['todos-main-top-left',{'allChecked':allChecked}]" @click.stop="handleCheck">></div>
        <div class="todos-main-top-right">
          <input type="text" v-model="inputVal" @keyup.enter="saveTodos"/>
        </div>
      </div>
      <!-- 顶部开始 -->
      <!-- 中间部分开始 -->
      <div class="todos-main-mid">
        <div class="todos-mid-list">
          <div class="mid-list-item" v-for="(item,index) in filterTodos" :key="item.id">
            <div class="list-item-left">
              <input type="checkbox" name="" id="" v-model="item.status">
            </div>
            <div :class="['list-item-mid',{completed:item.status}]" @dblclick="handleEditTodo(index)">
              <span :class="['titie',{hidden:edit_index === index}]">{{item.text}}</span>
              <input :class="[{hidden:edit_index !== index}]" type="text" v-model="item.text" @blur="saveEditTodo">
            </div>
            <div class="list-item-right" @click="deleteTodos(index)">×</div>
          </div>
        </div>
      </div>
      <!-- 中间部分结束 -->
      <!-- 底部开始 -->
      <div class="todos-main-footer">
        <div class="main-footer-left"><span>{{remained}}</span> 项剩下</div>
        <div class="main-footer-mid">
          <div @click="handleItemClick('all')" :class="['footer-mid-left',{active:activeTab === 'all'}]">All</div>
          <div @click="handleItemClick('active')" :class="['footer-mid-mid',{active:activeTab === 'active'}]">Active</div>
          <div @click="handleItemClick('ok')" :class="['footer-mid-right',{active:activeTab === 'ok'}]">OK</div>
        </div>
        <div class="main-footer-right" @click="handleClearOK">Clean Finish</div>
      </div>
      <!-- 底部结束 -->
    </div>
    <!-- 内容结束 -->
  </div>



</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
//import引入的组件需要注入到对象中才能使用
components: {},
data() {
//这里存放数据
return {
  activeTab:"all",
  // allChecked:false,
  edit_index:-1,
  inputVal:"",
  todos:[
    {
      text:"喜羊羊",
      status:true,
      id:"1"
    },
    {
      text:"懒羊羊",
      status:false,
      id:"2"
    }
  ]
};
},
//监听属性 类似于data概念
computed: {
  //剩下项目
  remained:function(){
    let remainTodos = this.todos.filter(v=>{
      return !v.status; //false返回
    })
    return remainTodos.length;
  },
  //过滤后的todos
  filterTodos:function(){
    if(this.activeTab === "all"){
      return this.todos;
    }else if(this.activeTab === "active"){
      return this.todos.filter(v=>{
        return !v.status;
      })
    }else if(this.activeTab === "ok"){
      return this.todos.filter(v=>{
        return v.status;
      })
    }
  },

  allChecked:function(){
    let checked = true;
    this.todos.map(v=>{
      if(!v.status){
        checked = false;
      }
    })
    return checked;
  }

},
//监控data中的数据变化
watch: {
  todos:{
    handler:function(newTodos){
      localStorage.setItem("todos",JSON.stringify(newTodos));
    }
  }
},
//方法集合
methods: {
  //清除完成项
  handleClearOK:function(){
    let activeTodos = this.todos.filter(v=>{
      return !v.status;
    })
    this.todos = activeTodos;
  },
  //底部功能栏
  handleItemClick:function(activeTab){
    this.activeTab = activeTab;
  },
  //编辑框失去焦点
  saveEditTodo:function(){
    this.edit_index = -1;
  },
  //双击编辑
  handleEditTodo:function(index){
    console.log("正在编辑第",index+1,"行");
    this.edit_index = index;
  },
  //删除todos
  deleteTodos:function(index){
    this.todos.splice(index,1);
  },
  //全选
  handleCheck:function(){
    // this.allChecked = true;
    //新拷贝一个todos
    let newTodos = JSON.parse(JSON.stringify(this.todos));
    //todos中的每一个状态都为true
    newTodos.map(v=>{
      v.status = true;
    })
    this.todos = newTodos;
  },
  saveTodos:function(){
    if(!this.inputVal){
      this.$message({
        message: '输入不能为空',
        type: 'warning'
      })
      return false;
    }
    this.todos.push({
      text:this.inputVal,
      status:false,
      id:Date.now(),
    })
  }
},
//生命周期 - 创建完成（可以访问当前this实例）
created() {

},
//生命周期 - 挂载完成（可以访问DOM元素）
mounted() {

},
beforeCreate() {}, //生命周期 - 创建之前
beforeMount() {}, //生命周期 - 挂载之前
beforeUpdate() {}, //生命周期 - 更新之前
updated() {}, //生命周期 - 更新之后
beforeDestroy() {}, //生命周期 - 销毁之前
destroyed() {}, //生命周期 - 销毁完成
activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang='less' scoped>
.todos {
  width: 50vw;
  margin: 0 auto;
  .todos-header {
    h1 {
      height: 60px;
      line-height: 60px;
      text-align: center;
      color: yellowgreen;
      font-size: 40px;
      font-family: 'Trebuchet MS';
    }
  }
  .todos-main {
    min-height: 500px;
    border: 1px solid #ddd;
    .todos-main-top {
      margin-top: 5px;
      padding: 5px;
      height: 30px;
      display: flex;
      .todos-main-top-left {
        height: 30px;
        flex: 1;
        text-align: center;
        cursor: pointer;
      }
      .allChecked{
        color: yellowgreen;
      }
      .todos-main-top-right {
        height: 30px;
        flex: 8;
        input {
          height: 100%;
          width: 100%;
        }
      }
    }

    .todos-main-mid {
      .todos-mid-list {
        .mid-list-item {
          height: 50px;
          display: flex;
          align-items: center;
          .list-item-left {
            display: flex;
            justify-content: center;
            flex:1;
            input {

            }
          }

          .list-item-mid {
            flex:8;
            .titie {
            
            }
          }
          .hidden{
            display: none;
          }
          .completed{
            color: #ddd;
            text-decoration: line-through;
          }
          .list-item-right {
            visibility: hidden;
            display: flex;
            justify-content: center;
            flex:1;
            color: yellowgreen;
          }
        }
        .mid-list-item:hover{
          .list-item-right{
            visibility: visible;
            display: flex;
            justify-content: center;
            flex: 1;
            cursor: pointer;
          }
        }
      }
    }

  }
  .todos-main-footer {
    height: 50px;
    padding: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .main-footer-left {
      span{
        color: yellowgreen;
      }
    }

    .main-footer-mid {
      display: flex;
      cursor: pointer;
      .footer-mid-left {
        margin-right: 10px;
      }

      .footer-mid-mid {
        margin-right: 10px;
      }

      .footer-mid-right {
        margin-right: 10px;
      }
    }
    .active{
      border: 2px solid yellowgreen;
      border-radius: 5px;
    }
    .main-footer-right {
      cursor: pointer;
    }
    .main-footer-right:hover{
      color: yellowgreen;
    }
  }
}
</style>

